/**
 * 验证手机号
 */
function is_phone(mobile) {
    {
        var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
        var tel = /^([0\+]\d{2,3}-)?(0\d{2,3}\-)?([2-9]\d{6,7})+(\-\d{1,4})?$/;
        if (!myreg.test(mobile) && !is_fax(mobile))
        {
            return false;
        } else {
            return true;
        }
    }

}

/**
 * 验证电话传真
 */
function is_fax(tel) {
    var myreg = /^([0\+]\d{2,3}-)?(0\d{2,3}\-)?([2-9]\d{6,7})+(\-\d{1,4})?$/;
    if (!myreg.test(tel))
    {
        return false;
    } else {
        return true;
    }
}



/**
 * 验证邮箱
 */
function is_email(email) {
    //对电子邮件的验证
    var myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
    if (!myreg.test(email))
    {
        return false;
    } else {
        return true;
    }
}


function form(form_id, submit_url, dataType) {
    dataType = dataType ? dataType : 'json';

    if (form_id == '' && submit_url == '') {
        error('Lack of necessary parameters');
        return false;
    }
    var Form_data = $('#' + form_id).serialize();
    $.ajax({
        type: "POST",
        url: submit_url,
        data: $('#' + form_id).serialize(), // 你的formid    
        dataType: dataType,
        error: function (request) {
            error("Server busy, please contact administrator!");
        },
        success: function (data) {
            error(data.msg);
            if (data.code == 1) {
                if (data.url) {
                    setTimeout(function () {
                        location.href = data.url;
                    }, 2000)
                }
            }
        }
    })
}

function form2(form_id, submit_url, dataType) {
    dataType = dataType ? dataType : 'json';

    if (form_id == '' && submit_url == '') {
        error('Lack of necessary parameters');
        return false;
    }
    var Form_data = $('#' + form_id).serialize();
    $.ajax({
        type: "POST",
        url: submit_url,
        data: $('#' + form_id).serialize(), // 你的formid    
        dataType: dataType,
        error: function (request) {
            $('#em_pas').removeAttr('disabled');
            error("Server busy, please contact administrator!");
        },
        success: function (data) {
            $('#em_pas').removeAttr('disabled');
            if (data.code == 1) {
                $('.msg').html(data.msg).removeClass('hidden text-danger').addClass('text-info');
            } else {
                $('.msg').html(data.msg).removeClass('hidden text-info').addClass('text-danger');
            }
        }
    })
}



/*系统提示*/
function error(msg) {
    $('#error_msg h4').html(msg);
    $('#error').trigger('click');
}


//登录
$('#login').on('click', function () {
    var account = $('input[name="account"]').val();
    var password = $('input[name="password"]').val();

    if (!account) {
        error('Please enter the username');
        return false;
    }

    if (!password) {
        error('Please enter your password');
        return false;
    }

    form('form', '/index/login/login');
})

//修改信息1
$('#info1').on('click', function () {
    var first_name = $('#info1_form input[name="first_name"]').val();
    var surnames = $('#info1_form input[name="surnames"]').val();
    var organization = $('#info1_form input[name="organization"]').val();

    if (!first_name) {
        error('please enter your first_name');
        return false;
    }
    if (!surnames) {
        error('please enter your surnames');
        return false;
    }
    if (!organization) {
        error('please enter your organization');
        return false;
    }

    form('info1_form', '/index/index/account');

})


//修改信息2
$('#info2').on('click', function () {
    var phone = $('#info2_form input[name="phone"]').val();
    var fax = $('#info2_form input[name="fax"]').val();
    var email = $('#info2_form input[name="email"]').val();

    if (phone) {
        if (!is_phone(phone)) {
            error('please enter your phone number');
            return false;
        }
    }

    if (fax) {
        if (!is_fax(fax)) {
            error('Your fax format is not correct. Please re-enter it.');
            return false;
        }
    }


    if (email) {
        if (!is_email(email)) {
            error('Your email address is not in the correct format, please enter again.');
            return false;
        }
    } else {
        error('Please enter your email address');
        return false;
    }

    form('info2_form', '/index/index/account');

})

//修改地址
$('#addr').on('click', function () {
    var addr1 = $('#addr_form input[name="addr1"]').val();
    var city = $('#addr_form input[name="city"]').val();
    var country = $('#addr_form input[name="country"]').val();

    if (!addr1) {
        error('Please enter an addr1');
        return false;
    }
    if (!city) {
        error('Please enter a city');
        return false;
    }
    if (!country) {
        error('please enter a country');
        return false;
    }
    form('addr_form', '/index/index/addr');

})

//后续课程
$('#activity').on('click', function () {

    form('activity_form', '/index/index/certificate');

})

//退出登录
$('#loginout').on('click', function () {
    $.getJSON('/index/login/loginout', function (data) {
        error(data.msg);
        if (data.code == 1) {
            if (data.url) {
                setTimeout(function () {
                    location.href = data.url;
                }, 2000);
            }
        }
    })
})


//注册
$('#reg').on('click', function () {
    var first_name = $('#reg_form input[name="first_name"]').val();
    var surnames = $('#reg_form input[name="surnames"]').val();
    var phone = $('#reg_form input[name="phone"]').val();
    var email = $('#reg_form input[name="email"]').val();

    if (!first_name) {
        error('please enter your first_name');
        return false;
    }
    if (!surnames) {
        error('please enter your surnames');
        return false;
    }
    if (!phone) {
        error('please enter your phone number');
        return false;
    }
    if (email) {
        if (!is_email(email)) {
            error('Your email address is not in the correct format, please enter again.');
            return false;
        }
    } else {
        error('Please enter your email address');
        return false;
    }

    form('reg_form', '/index/login/reg');

})

//注册2
$('.level').on('click', function () {
    var level_id = $(this).data('level');
    if (level_id) {
        $('#level').val(level_id);
        form('reg1_form', '/index/login/reg');
    }
})

//注册3
$('#reg3').on('click', function () {
    var addr1 = $('#reg3_form input[name="addr1"]').val();
    var city = $('#reg3_form input[name="city"]').val();
    var country = $('#reg3_form input[name="country"]').val();

    if (!addr1) {
        error('Please enter an addr1');
        return false;
    }
    if (!city) {
        error('Please enter a city');
        return false;
    }
    if (!country) {
        error('please enter a country');
        return false;
    }
    form('reg3_form', '/index/login/reg');

})


//注册4
$('.is_true').on('click', function () {
    var is_true = $(this).data('is');
    if (is_true !== '') {
        $('#is_true').val(is_true);
        form('reg4_form', '/index/login/reg');
    }
})

//是否犯罪
$('.is_crime').on('click', function () {
    var is_crime = $(this).data('is');
    $(this).parent().find('a').fadeOut(200);
    if (is_crime !== 0) {
        $('#is_crime').val(is_crime);
        $('#is_true2').val(0);
        form('crime_form', '/index/login/reg');
    } else {
        $('#is_crime2').val(0);
        $('.is_show').toggleClass('hidden');
    }
})

//注册3
$('#reg5').on('click', function () {

    form('reg5_form', '/index/login/reg');

})

//修改密码
$('#password').on('click', function () {
    var password = $('input[name="password"]').val();

    if (!password) {
        error('Please enter your old password');
        return false;
    }

    form('password_form', '/index/index/password');

})

//发送邮件
$('#em_pas').on('click', function () {
    var email = $('input[name="email"]').val();

    if (email) {
        if (!is_email(email)) {
            error('Your email address is not in the correct format, please enter again.');
            return false;
        }
    } else {
        error('Please enter your email address');
        return false;
    }
    $('#em_pas').attr('disabled', 'disabled');

    form2('em_pas_form', '/index/login/email');
})

$('#error_msg').on('click', '#click', function () {
    if ($('.bs-example-modal-sm').attr('aria-hidden') == 'false')
        $('.bs-example-modal-sm').trigger('click');

    $('.modal-backdrop').remove();
})


/*修改密码*/
$('#em_pas2').on('click', function () {
    form('em_pas_form2', '/index/login/pass');
})

//cpe视频
$('#exampleModal2').on('show.bs.modal', function (event) {
    var _event = event;
    var _this = this;
    //检查权限是否可以播放
    $.post('/index/index/void', function (data) {
        if (data.status === 0) {
            var modal = $(_this)
            modal.find('.modal-title').text('System prompt')
            modal.find('.modal-body ').empty().addClass('text-danger').html('<h4 class="text-center">' + data.message + '</h4>')
            modal.find('.modal-footer ').hide()
            return false;
        } else {
            var button = $(_event.relatedTarget)
            var recipient = button.data('whatever')
            var title = button.parent().prev().prev().text();

            var modal = $(_this)

//            
//            $(".vv2").attr("id", "polyvplayer" + recipient);
//            $(".vv3").attr("flashvars", "vid=" + recipient);
//            $(".vv3").attr("name", "polyvplayer" + recipient);

            modal.find('.modal-title').text(title)
            modal.find('.modal-body .void').attr('id', 'plv_' + recipient);
            play(recipient);
        }
    }, 'json');
})


//$('.find_code').on('click', function (e) {
//    e.preventDefault();
//    $.post('/index/find/index', $("#find_form").serialize(), function (data) {
//        if (data.status == 1) {
//            
//        }else{
//            error(data.message);
//        }
//    }, 'json')
//})







